package com.snakegame.ui;

import com.snakegame.core.GameManager;
import com.snakegame.config.Difficulty;
import com.snakegame.config.GameState;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 难度选择面板，允许用户选择游戏难度
 */
public class DifficultySelectionPanel extends JPanel {
    private GameManager gameManager;
    private GameFrame gameFrame;
    
    public DifficultySelectionPanel(GameManager gameManager, GameFrame gameFrame) {
        this.gameManager = gameManager;
        this.gameFrame = gameFrame;
        
        setLayout(new BorderLayout());
        setBackground(Color.BLACK);
        
        // 标题
        JLabel titleLabel = new JLabel("选择游戏难度");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        add(titleLabel, BorderLayout.NORTH);
        
        // 难度选择区域
        JPanel difficultyPanel = new JPanel();
        difficultyPanel.setLayout(new GridLayout(0, 1, 10, 10));
        difficultyPanel.setBackground(Color.BLACK);
        difficultyPanel.setBorder(BorderFactory.createEmptyBorder(50, 100, 50, 100));
        
        ButtonGroup difficultyGroup = new ButtonGroup();
        
        // 简单难度
        JRadioButton easyButton = new JRadioButton("简单");
        easyButton.setFont(new Font("宋体", Font.PLAIN, 18));
        easyButton.setForeground(Color.WHITE);
        easyButton.setBackground(Color.BLACK);
        easyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setDifficulty(Difficulty.EASY);
            }
        });
        
        // 中等难度
        JRadioButton mediumButton = new JRadioButton("中等");
        mediumButton.setFont(new Font("宋体", Font.PLAIN, 18));
        mediumButton.setForeground(Color.WHITE);
        mediumButton.setBackground(Color.BLACK);
        mediumButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setDifficulty(Difficulty.MEDIUM);
            }
        });
        
        // 困难难度
        JRadioButton hardButton = new JRadioButton("困难");
        hardButton.setFont(new Font("宋体", Font.PLAIN, 18));
        hardButton.setForeground(Color.WHITE);
        hardButton.setBackground(Color.BLACK);
        hardButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setDifficulty(Difficulty.HARD);
            }
        });
        
        // 根据当前难度设置默认选中
        switch (gameManager.getDifficulty()) {
            case EASY:
                easyButton.setSelected(true);
                break;
            case MEDIUM:
                mediumButton.setSelected(true);
                break;
            case HARD:
                hardButton.setSelected(true);
                break;
        }
        
        difficultyGroup.add(easyButton);
        difficultyGroup.add(mediumButton);
        difficultyGroup.add(hardButton);
        
        difficultyPanel.add(easyButton);
        difficultyPanel.add(mediumButton);
        difficultyPanel.add(hardButton);
        
        // 说明文字
        JLabel instructionLabel = new JLabel("难度说明:");
        instructionLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        instructionLabel.setForeground(Color.WHITE);
        instructionLabel.setHorizontalAlignment(JLabel.LEFT);
        difficultyPanel.add(instructionLabel);
        
        JLabel easyLabel = new JLabel("简单: 速度慢，分数倍数低");
        easyLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        easyLabel.setForeground(Color.GRAY);
        easyLabel.setHorizontalAlignment(JLabel.LEFT);
        difficultyPanel.add(easyLabel);
        
        JLabel mediumLabel = new JLabel("中等: 速度适中，分数倍数中等");
        mediumLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        mediumLabel.setForeground(Color.GRAY);
        mediumLabel.setHorizontalAlignment(JLabel.LEFT);
        difficultyPanel.add(mediumLabel);
        
        JLabel hardLabel = new JLabel("困难: 速度快，分数倍数高");
        hardLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        hardLabel.setForeground(Color.GRAY);
        hardLabel.setHorizontalAlignment(JLabel.LEFT);
        difficultyPanel.add(hardLabel);
        
        add(difficultyPanel, BorderLayout.CENTER);
        
        // 底部按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setBackground(Color.BLACK);
        
        // 返回按钮
        JButton backButton = new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameState(GameState.START_MENU);
                gameFrame.showPanel("START_MENU");
            }
        });
        buttonPanel.add(backButton);
        
        // 确定按钮
        JButton confirmButton = new JButton("确定");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameState(GameState.START_MENU);
                gameFrame.showPanel("START_MENU");
            }
        });
        buttonPanel.add(confirmButton);
        
        add(buttonPanel, BorderLayout.SOUTH);
    }
}    